package com.nice.nice201110.service;

import com.nice.nice201110.entity.TBasePicture;
import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.mapper.TBasePictureMapper;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TbasePictureService {

    @Autowired
    TBasePictureMapper tBasePictureMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /** 查询数据 */
    public List<TBasePicture> getList(BaseVo baseVo){
        return tBasePictureMapper.selectByPrimaryKeys(baseVo);
    }
    /** 单一查询  */
    public TBasePicture getOne(UserVo userVo,Integer id){
        TBasePicture tBasePicture = tBasePictureMapper.selectByPrimaryKey(id);
        if(tBasePicture!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tBasePicture.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_base_picture);
            tLog.setRemark("查看药品信息:"+tBasePicture.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tBasePicture;
    }
    /** 更新数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean updateTbasePicture(UserVo userVo,TBasePicture tBasePicture){
        try {
            int num = tBasePictureMapper.updateByPrimaryKeySelective(tBasePicture);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(1);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBasePicture.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_picture);
                tLog.setRemark("修改药品信息:"+tBasePicture.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
    /** 插入信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean insertTbasePicture(UserVo userVo,TBasePicture tBasePicture){
        try {
            int num = tBasePictureMapper.insertSelective(tBasePicture);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tBasePicture.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_picture);
                tLog.setRemark("新增药品信息:"+tBasePicture.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;

    }
    /** 删除信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deleteTbasePicture(UserVo userVo,Integer id){
        try {
            int num = tBasePictureMapper.deleteSelective(id);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(id);
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_base_picture);
                tLog.setRemark("删除药品信息:" + tBasePictureMapper.selectByPrimaryKey(id).toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;

    }
    /** 删除多数据 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean deletesTbasePicture(UserVo userVo, String ids){
        String StrArr [] = ids.split(",");
        try {
        for(String strID:StrArr) {
            if(strID!=null && strID.trim().length()>0) {
                int id = Integer.valueOf(strID);
                int num = tBasePictureMapper.deleteSelective(id);
                if (num > 0) {
                    tLog.setDate(new Date());
                    tLog.setType(2);  //操作类型（新增0，修改1，删除2）
                    tLog.setDocid(id); //  数据id
                    tLog.setUsername(userVo.getName());
                    tLog.setUserid(Integer.valueOf(userVo.getId()));
                    tLog.setTtable(NiceTableEnum.form_base_picture);
                    tLog.setRemark("删除药品信息:" + tBasePictureMapper.selectByPrimaryKey(id).toString());
                    tLogMapper.insertSelective(tLog);
                }else{
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return false;
                }
            }
        }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
